/**  
 * @author renbangjie renbangjie@126.com  
 * @date 2015-9-24 上午10:37:35
 * @version V1.0  
 * <b>Copyright (c)</b> 2014医信金融信息服务（上海）有限公司-版权所有<br/>
 */
package com.yjd.comm.util;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.patchca.background.SingleColorBackgroundFactory;
import org.patchca.color.ColorFactory;
import org.patchca.color.SingleColorFactory;
import org.patchca.filter.predefined.CurvesRippleFilterFactory;
import org.patchca.font.RandomFontFactory;
import org.patchca.service.Captcha;
import org.patchca.service.ConfigurableCaptchaService;
import org.patchca.text.renderer.BestFitTextRenderer;
import org.patchca.text.renderer.TextRenderer;
import org.patchca.word.RandomWordFactory;

import com.yjd.comm.service.ISecurityCodeService;

/**
 * 验证码工具
 * 
 * @author renbangjie renbangjie@126.com
 * @date 2015-9-24 上午10:37:35
 */
public class CaptchaUtils {

	/**
	 * 验证码集合
	 */
	private static final String CAPTCHA_BASE_CODE = "23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ";

	// captcha验证码
	private static ConfigurableCaptchaService captchaService = new ConfigurableCaptchaService();

	// 验证码随机颜色
	private static final Color[] colors = { Color.blue, Color.black, new Color(100, 149, 237), new Color(30, 144, 255) };

	/**
	 * 生成验证码
	 * 
	 * @param length
	 *            字符数
	 * @param fontSize
	 *            字体大小
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @param securityCodeService
	 * @return BufferedImage
	 * @throws Exception
	 */
	public static BufferedImage getCaptcha(int length,
			ISecurityCodeService securityCodeService) throws Exception {

		// 设置相关属性
		setProperty(45, length, 3, 0, 0);

		// 得到验证码对象
		Captcha captcha = captchaService.getCaptcha();

		// 放入缓存
		securityCodeService.captcha(captcha.getChallenge());

		return captcha.getImage();

	}

	/**
	 * 设置验证码相关属性
	 * 
	 * @param fsize
	 *            字体大小
	 * @param length
	 *            字符长度
	 * @param margin
	 *            上下间距
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @throws Exception
	 *             void
	 */
	private static void setProperty(int fsize, int length, int margin,
			int width, int height) throws Exception {

		captchaService.setColorFactory(getColorFactory());

		RandomFontFactory ff = new RandomFontFactory();
		ff.setMaxSize(fsize);
		ff.setMinSize(fsize);
		captchaService.setFontFactory(ff);

		RandomWordFactory wf = new RandomWordFactory();
		wf.setCharacters(CAPTCHA_BASE_CODE);
		wf.setMaxLength(length);
		wf.setMinLength(length - 1);
		captchaService.setWordFactory(wf);

		captchaService.setFilterFactory(new CurvesRippleFilterFactory(
				captchaService.getColorFactory()));

		// 文字渲染器设置
		TextRenderer textRenderer = new BestFitTextRenderer();
		textRenderer.setBottomMargin(margin);
		textRenderer.setTopMargin(margin);
		captchaService.setTextRenderer(textRenderer);

		// 验证码图片的大小
		// captchaService.setWidth(width);
		// captchaService.setHeight(height);

		// 背景颜色
		captchaService.setBackgroundFactory(new SingleColorBackgroundFactory(
				Color.WHITE));
	}

	/**
	 * 获取随机颜色
	 * @return ColorFactory
	 * @throws Exception
	 */
	private static ColorFactory getColorFactory() throws Exception {
		// return new RandomColorFactory();

		int random = new Random().nextInt(colors.length);
		return new SingleColorFactory(colors[random]);
	}

	/**
	 * 初始化
	 * 
	 * @param colors
	 *            颜色集合
	 * @throws Exception
	 *             void
	 */
	private static void init(Color... colors) {
		List<ColorFactory> colorList = new ArrayList<ColorFactory>();
		for (Color color : colors) {
			colorList.add(new SingleColorFactory(color));
		}
	}

}
